//
//  RYAddressChoiceView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/20.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

class RYAddressChoiceView: SYBaseView   {
    
    var selectBiaoqianClosure: ((Int) -> Void)?
    var bgBt: UIButton = {
        let v = UIButton()
        v.backgroundColor = UIColor(white: 0, alpha: 0.5)
        return v
    }()
    
    fileprivate var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = .white
        return v
    }()

    var fir: RYAddressTable = {
        let v: RYAddressTable = RYAddressTable()
        v.backgroundColor = UIColor(hex: 0xF4F4F4)
        return v
    }()
    
    var sec: RYAddressTable = {
        let v: RYAddressTable = RYAddressTable()
        v.backgroundColor = UIColor(hex: 0xF4F4F4)
        v.isHidden = true
        return v
    }()
    
    var thir: RYAddressTable = {
        let v: RYAddressTable = RYAddressTable()
        v.backgroundColor = UIColor(hex: 0xF4F4F4)
        v.isHidden = true
        return v
    }()
    
    var four: RYAddressTable = {
        let v: RYAddressTable = RYAddressTable()
        v.backgroundColor = UIColor(hex: 0xF4F4F4)
        v.isHidden = true
        return v
    }()
    
    var tables: [RYAddressTable] = []

    let keywindow = UIApplication.shared.keyWindow

    var biaoqian: RYBiaoQianView = RYBiaoQianView(frame: CGRect(x: 0, y: SCREEN_HEIGHT/2, width: SCREEN_WIDTH, height: 44))
    
    public func configureView(datas: [[String: String]], cengji: Int, selectArea: [String]) {
        
        if datas.count == 0 {
            self.isHidden = true
            return
        }
        for (i, value) in tables.enumerated() {
            if i == cengji {
                value.isHidden = false
                value.datas = datas
                value.cengji = cengji
            } else {
                value.isHidden = true
            }
        }
        biaoqian.resetViews(selectArea, cengji)
    }
 
    
    func show() {
        self.isHidden = false
        
    }
    
    func hide() {
        self.isHidden = true
        
    }


    override func initViews() {
        
        tables = [fir, sec, thir, four]
        biaoqian.configureView(titles: ["请选择"])
        biaoqian.clickedClosure = {[weak self] index in
            if let `self` = self {
//                        self.selectBiaoqianClosure?(index)
//                for (i, value) in self.tables.enumerated() {
//                    if i == index {
//                        value.isHidden = false
//                    } else {
//                        value.isHidden = true
//                    }
//                }
            }
            
        }
        keywindow?.addSubview(self)
        biaoqian.backgroundColor = .white
        
        self.isHidden = true
        bgBt.addAction {[weak self] in
            if let `self` = self {
                self.isHidden = true
            }
        }
        addSubview(bgBt)
        addSubview(bgView)
        addSubview(biaoqian)
        addSubview(fir)
        addSubview(sec)
        addSubview(thir)
        addSubview(four)
        
        bgBt.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(biaoqian.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        fir.snp.makeConstraints { (make) in
            make.top.equalTo(biaoqian.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        sec.snp.makeConstraints { (make) in
            make.top.equalTo(biaoqian.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        thir.snp.makeConstraints { (make) in
            make.top.equalTo(biaoqian.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        four.snp.makeConstraints { (make) in
            make.top.equalTo(biaoqian.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
}


class RYAddressTable: SYBaseView, UITableViewDelegate, UITableViewDataSource {
    
    public var selectClosure: ((String, String, Int) -> Void)?
    var cengji: Int = 0 // 省市县镇 层级
    var tableView: UITableView!
    var selectRow: Int?
    var datas: [[String: String]] = [] {
        didSet {
            self.tableView.reloadData()
        }
    }
    
    override func initViews() {
        setTableView()
        addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView!.register(RYAddressTableCell.self, forCellReuseIdentifier: "Cell")
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: RYAddressTableCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? RYAddressTableCell
        cell?.selectionStyle = .none
        cell?.titleLb.text = datas[indexPath.row]["name"]
        if let r = selectRow, indexPath.row == r {
            cell?.titleLb.textColor = UIColor(hex: 0x367FF6)
        } else {
            cell?.titleLb.textColor = UIColor(hex: 0x333333)
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt")
        selectRow = indexPath.row
        self.selectClosure?(datas[indexPath.row]["id"] ?? "", datas[indexPath.row]["name"] ?? "", cengji + 1)
        self.tableView.reloadData()
    }
}


class RYAddressTableCell: SYBaseCell {
    
    let titleLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 14.autoFontSize()
        return v
    }()

    
    override func initViews() {
        addSubview(titleLb)
        titleLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15.autoWidth())
            make.height.equalTo(15.autoWidth())
            make.top.equalToSuperview().offset(10.autoWidth())
            make.bottom.equalToSuperview().offset(-10.autoWidth())
        }
    }
}
